﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HomeWork3_1
{
    //链表节点
    public class Node<T>
    {
        public Node<T> Next { get; set; }
        public T Data { get; set; }
        public Node(T t)
        {
            Next = null;
            Data = t;
        }
    }
    //泛型链表类
    public class Genericlist<T>
    {
        private Node<T> head;
        private Node<T> tail;
        public Genericlist()
        {
            tail = head = null;
        }
        public Node<T> Head
        {
            get => head;
        }
        public void Add(T t)
        {
            Node<T> n = new Node<T>(t);
            if (tail == null)
            {
                head = tail = n;
            }
            else
            {
                tail.Next = n;
                tail = n;
            }
        }
        public void ForEach(Action<T> action)
        {
            Node<T> p = head;
            while (p != null)
            {
                action(p.Data);
                p = p.Next;
            }


        }
    }


        internal class Program
        {
            static void Main(string[] args)
            {
                //整型list
                Genericlist<int> intlist = new Genericlist<int>();
                for (int x = 0; x < 10; x++)
                {
                    intlist.Add(x);
                }
                //字符串型List
                Genericlist<string> strList = new Genericlist<string>();
                for (int x = 0; x < 10; x++)
                {
                    strList.Add("str" + x);
                }
                intlist.ForEach(m => Console.WriteLine(m));//打印元素
                //求最大值
                int max = intlist.Head.Data;
                intlist.ForEach(m =>
                {
                    if (m > max)
                        max = m;
                });
                Console.WriteLine($"最大值：{max}");
                //求最小值
                int min = intlist.Head.Data;
                intlist.ForEach(m =>
                {
                    if (m < min)
                        min = m;
                });
                Console.WriteLine($"最小值：{min}");
                //求和
                int sum = 0;
                intlist.ForEach(m => sum += m);
                Console.WriteLine($"和：{sum}");
                Console.ReadKey();
             }
        }
}
